package cn.mogugo.joke.manager;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.Bullet;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import cn.mogugo.comm.BaseManager;
import cn.mogugo.joke.model.JokeCategory;
import cn.mogugo.joke.model.JokeObj;
import cn.mogugo.joke.model.JokeWeb;
import cn.mogugo.util.tools.DateTools;
import cn.mogugo.util.tools.StringTools;

public class JokeWebManager   extends BaseManager{

	public List getAll(){
		String hql = "from JokeWeb jw order by jw.id asc";
		return this.baseDao.getByHQL(hql, null);
	}

	
	public void saveJokeFromZOL(JokeWeb jw , Integer ii){
		
		try{
			String dataSource = jw.getData_source();
			String url = StringTools.setParameters(dataSource, "pageNo", ii+"");
			
			//System.out.println(url);
			
			Parser parser = new Parser(url);
			parser.setEncoding("gb2312");
			
			NodeFilter divFilter = new NodeClassFilter(Div.class);
			NodeFilter classFilter = new HasAttributeFilter("class","left_font");
			AndFilter andFilter = new AndFilter(divFilter , classFilter);
			NodeFilter hasParentFilter = new HasParentFilter(andFilter);
			
			
			NodeFilter linkFilter = new AndFilter(new NodeClassFilter(LinkTag.class) , hasParentFilter);
			
			
			NodeList nodeList = parser.parse(linkFilter);
			JokeObj[] jos = new JokeObj[nodeList.size()];
			for(int i = 0 ; i < nodeList.size() ; i ++ ){
				JokeObj jokeObj = new JokeObj();
				jokeObj.setId(null);
				jokeObj.setAuditing(0);
				jokeObj.setClick_number(0);
				jokeObj.setJokeWeb(jw);
				
				
				LinkTag node = (LinkTag)nodeList.elementAt(i);
				String href = "http://xiaohua.zol.com.cn" + node.getAttribute("href");
				jokeObj.setJoke_title(node.getAttribute("title"));
				
				Parser parserJokeObj = new Parser(href);
				parserJokeObj.setEncoding("gb2312");
				
				NodeFilter divChildFilter = new NodeClassFilter(Div.class);
				NodeFilter classChildFilter = new HasAttributeFilter("class","lastC");
				AndFilter andChildFilter = new AndFilter(divChildFilter , classChildFilter);
				
				NodeFilter fontChildFilter2 = new TagNameFilter("font");
				NodeFilter classChildFilter2 = new HasAttributeFilter("color","#ff0000");
				NodeFilter andChildFilter2 = new AndFilter(fontChildFilter2 , classChildFilter2);
				NodeFilter orFilter = new OrFilter(andChildFilter , andChildFilter2);
				
				NodeList childNodeList = parserJokeObj.parse(orFilter);
				Node jokeObjNode = childNodeList.elementAt(0);
				String content = jokeObjNode.toPlainTextString().replaceAll("\\s+", "\n").replaceAll("&nbsp;", "");
				while(content.startsWith("\n")){
					content = content.replaceFirst("\n", "");
				}
				
				jokeObj.setJoke_content(content);
				
				Node jokeObjNode2 = childNodeList.elementAt(1);
				if(jokeObjNode2 != null){
					String jokeCategoryName = jokeObjNode2.getNextSibling().getText();
					
					if(StringUtils.isNotBlank(jokeCategoryName)){
						JokeCategory jc = this.jokeCategoryDao.getByCategoryName(jokeCategoryName);
						if(jc == null){
							jc = this.jokeCategoryDao.saveNewCategory(jokeCategoryName);
						}
						jokeObj.setJokeCategory(jc);
					}
				}

				jokeObj.setJoke_date(DateTools.getCurDateTime());
				jos[i] = jokeObj;
				
				//Thread.sleep(500);
			}
			this.baseDao.saveOrUpdateFlush(jos);

			System.gc();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
	
	
	public void saveJokeFromhaha365(JokeWeb jw , Integer ii){
		
		try{
			String dataSource = jw.getData_source();
			String url = StringTools.setParameters(dataSource, "pageNo", ii+"");
			
			//System.out.println(DateTools.getCurDateTimeString() + "  " + url);
			
			Parser parser = new Parser(url);
			parser.setEncoding("gbk");
			
			NodeFilter li = new TagNameFilter("li");
			
			NodeFilter ul = new TagNameFilter("ul");
			NodeFilter ulClass = new HasAttributeFilter("class","text_doublelist1");
			AndFilter andUl = new AndFilter(ul , ulClass);
			NodeFilter liParent = new HasParentFilter(andUl);
			
			NodeFilter liAnd = new AndFilter(li , liParent);
			
			NodeList nodeList = parser.parse(liAnd);
			for(int i = 0 ; i < nodeList.size() ; i ++ ){
				JokeObj jokeObj = new JokeObj();
				jokeObj.setId(null);
				jokeObj.setAuditing(0);
				jokeObj.setClick_number(0);
				jokeObj.setJokeWeb(jw);
				
				
				Bullet b = (Bullet)nodeList.elementAt(i);
				NodeList listNodeList = b.getChildren();
				int linkAtPosition = 1;
				for(int j = 0 ; j < listNodeList.size() ; j ++ ){

					
					Node node = listNodeList.elementAt(j);
					if(node.getClass().getName().equals("org.htmlparser.tags.LinkTag")){
						if(linkAtPosition == 1){
							//分类
							LinkTag lt = (LinkTag)node;
							String tmp = lt.toPlainTextString();
							String categoryName = tmp.substring(1, tmp.length()-1);
							
							if(StringUtils.isNotBlank(categoryName)){
								JokeCategory jc = this.jokeCategoryDao.getByCategoryName(categoryName);
								if(jc == null){
									jc = this.jokeCategoryDao.saveNewCategory(categoryName);
								}
								jokeObj.setJokeCategory(jc);
							}
							linkAtPosition ++;
						}else if(linkAtPosition == 2){
							//笑话标题
							LinkTag lt = (LinkTag)node;
							String jokeTitle = lt.toPlainTextString();
							jokeObj.setJoke_title(jokeTitle);
							String uri = lt.getAttribute("href");
							String jokeUrl = "http://www.haha365.com" + uri;
							
							jokeObj.setFrom_uri(uri);
							
							NodeFilter objDiv = new TagNameFilter("div");
							NodeFilter objClass = new HasAttributeFilter("id","endtext");
							AndFilter objAnd = new AndFilter(objDiv , objClass);
							
							
							Parser objParser = new Parser(jokeUrl);
							objParser.setEncoding("gbk");
							
							NodeList objNodeList = objParser.parse(objAnd);
							Node objNode = objNodeList.elementAt(0);
							String content = objNode.toPlainTextString().replaceAll("\\s+", "<br>").replaceAll("　", "");
							while(content.startsWith("<br>")){
								content = content.replaceFirst("<br>", "");
							}
							jokeObj.setJoke_content(content);
						}
					}
				}
				jokeObj.setJoke_date(DateTools.getCurDateTime());
				this.baseDao.saveOrUpdate(jokeObj);
				
			}
			
			

			System.gc();
		
			

			
			
			
		}catch(Exception e){
			e.printStackTrace();
		}

	}
}
